<template>
  <div style="width: 60%">
    <el-form ref="form" :model="form" label-width="80px">
      <el-input v-model="form.uid" style="display: none"></el-input>
      <el-form-item label="昵称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>

      <el-form-item label="邮箱">
        <el-input v-model="form.email"></el-input>
      </el-form-item>

      <el-form-item label="账户">
        <el-input v-model="form.username"></el-input>
      </el-form-item>

      <el-form-item label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>

      <el-form-item label="创建时间">
        <el-col :span="11">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.modifytime"
            style="width: 100%;"
          ></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">修改</el-button>
        <!-- <el-button @click="onBack">返回</el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'UserEdit',
  mounted() {
    let uid = this.$route.query.uid;
    // 发送请求获取当前的数据
    axios.get("http://localhost:8002/user/loginUser", {
      params: { "uid": uid }
    }).then(response => {
      console.log(response);
      this.form = response.data.data;
    }).catch(error => {
      console.error("获取用户信息失败:", error);
    });
  },
  data() {
    return {
      form: {
        uid: null,
        name: '',
        email: '',
        username: '',
        password: '',
        modifytime: ''
      }
    }
  },
  methods: {
    onBack() {
      // 返回上一页
      this.$router.go(-1);
    },
    onSubmit() {
        // 发送网络请求
        axios.put("http://localhost:8002/user/updateUserById", this.form)
          .then(response => {
            console.log(response);
            if (response.data.data > 0) {
              this.$message.success("用户信息更新成功");
              // 跳转回用户信息页面
              this.$router.push("/userInfo");
            } else {
              this.$message.error("更新失败: " + response.data.message);
            }
          })
          .catch(error => {
            console.error("更新用户信息失败:", error);
            this.$message.error("更新失败，请稍后再试");
          });
    }
  }
}
</script>

<style scoped></style>
